package com.ruoyi.project.configureSystem.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.RandomTool;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.configureSystem.domain.SysNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.configureSystem.mapper.CCompanyMapper;
import com.ruoyi.project.configureSystem.domain.CCompany;
import com.ruoyi.project.configureSystem.service.ICCompanyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公司信息Service业务层处理
 *
 * @author wangshuo
 * @date 2021-04-12
 */
@Service
public class CCompanyServiceImpl implements ICCompanyService
{
    @Autowired
    private CCompanyMapper cCompanyMapper;

    /**
     * 查询公司信息
     *
     * @param id 公司信息ID
     * @return 公司信息
     */
    @Override
    public CCompany selectCCompanyById(String id)
    {
        return cCompanyMapper.selectCCompanyById(id);
    }

    /**
     * 查询公司信息列表
     *
     * @param cCompany 公司信息
     * @return 公司信息
     */
    @Override
    public List<CCompany> selectCCompanyList(CCompany cCompany)
    {
        List<CCompany> cCompanies = cCompanyMapper.selectCCompanyList(cCompany);
        return cCompanies;
    }

    /**
     * 新增公司信息
     *
     * @param cCompany 公司信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCCompany(CCompany cCompany)
    {
        cCompany.setId("C"+ RandomTool.generatorNum());
        cCompany.setCreateTime(DateUtils.getNowDate());
        cCompany.setUpdateBy(SecurityUtils.getUsername());
        if(cCompany.getParentId()==null){
            cCompany.setParentId("0");
        }else {
            CCompany cCompany1 = cCompanyMapper.selectCCompanyById(cCompany.getParentId());
            String ancestors = cCompany1.getAncestors();
            //拼接anstors
            if(StringUtils.isNotBlank(ancestors)){
                cCompany.setAncestors(ancestors+","+cCompany1.getId());
            }else {
                cCompany.setAncestors(cCompany1.getId());
            }
        }
        return cCompanyMapper.insertCCompany(cCompany);
    }

    /**
     * 修改公司信息
     *
     * @param cCompany 公司信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCCompany(CCompany cCompany)
    {
        cCompany.setUpdateTime(DateUtils.getNowDate());
        cCompany.setUpdateBy(SecurityUtils.getUsername());
        if(cCompany.getParentId()==null||"0".equals(cCompany.getParentId())){
            cCompany.setParentId("0");
        }else {
            CCompany cCompany1 = cCompanyMapper.selectCCompanyById(cCompany.getParentId());
            String ancestors = cCompany1.getAncestors();
            //拼接anstors
            if(StringUtils.isNotBlank(ancestors)){
                cCompany.setAncestors(ancestors+","+cCompany1.getId());
            }else {
                cCompany.setAncestors(cCompany1.getId());
            }
        }
        return cCompanyMapper.updateCCompany(cCompany);
    }

    /**
     * 批量删除公司信息
     *
     * @param ids 需要删除的公司信息ID
     * @return 结果
     */
    @Override
    public int deleteCCompanyByIds(String[] ids)
    {
        Arrays.stream(ids).forEach(x->{
            CCompany cCompany = new CCompany();
            cCompany.setParentId(x);
            List<CCompany> cCompanies = cCompanyMapper.selectCCompanyList(cCompany);
            if(cCompanies.size()>0){
                throw new BaseException("存在下级公司不允许删除");
            }
        });
        return cCompanyMapper.deleteCCompanyByIds(ids);
    }

    /**
     * 删除公司信息信息
     *
     * @param id 公司信息ID
     * @return 结果
     */
    @Override
    public int deleteCCompanyById(String id)
    {
        return cCompanyMapper.deleteCCompanyById(id);
    }

    @Override
    public List<SysNode> selectCCompanyNodeList(CCompany cCompany) {
        return cCompanyMapper.selectCCompanyNodeList(cCompany);
    }
}
